<template>
  <div>
    <!-- 面包屑导航区域 -->
    <Break></Break>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-col :span="4">
        <el-button type="primary" @click="addDialogVisible = true"
          >添加角色</el-button
        >
      </el-col>
      <el-table :data="roleList" border style="width: 100%">
        <el-table-column type="expand">
          <el-row slot-scope="scope">
            <el-row
              v-for="item in scope.row.children"
              :key="item.id"
              style="border-bottom: 1px solid #eee"
            >
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeById(scope.row, item.id)">{{
                  item.authName
                }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限和三级权限 -->
              <el-col :span="19">
                <el-row
                  v-for="item1 in item.children"
                  :key="item1.id"
                  style="border-bottom: 1px solid #eee"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      type="success"
                      @close="removeById(scope.row, item1.id)"
                      >{{ item1.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="14">
                    <el-tag
                      type="warning"
                      v-for="item2 in item1.children"
                      :key="item2.id"
                      closable
                      @close="removeById(scope.row, item2.id)"
                    >
                      {{ item2.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </el-row>
        </el-table-column>
        <el-table-column type="index" label="#" width="100px">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="180">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" width="180">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑区域 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEdidRole(scope.row)"
              >编辑</el-button
            >
            <!-- 删除区域 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeDelRole(scope.row.id)"
              >删除</el-button
            >
            <!-- 分配权限区域 -->
            <el-button
              type="warning"
              effect="dark"
              icon="el-icon-star-off"
              size="mini"
              @click="rightRole(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 编写弹出框 -->
    <el-dialog title="修改角色" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="editRuleForm"
        :rules="editRules"
        ref="ruleFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editRuleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editRuleForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRoles()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加弹出框 -->
    <el-dialog title="添加角色" :visible.sync="addDialogVisible" width="30%">
      <el-form
        :model="addRuleForm"
        :rules="editRules"
        ref="ruleFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRuleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addRuleForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限弹出框 -->
    <el-dialog title="分配权限" :visible.sync="rightDialogVisible" width="50%" >
      <el-tree
        :data="rightList"
        show-checkbox
        default-expand-all
        node-key="id"
        ref="treeRef"
        highlight-current
        :props="treeProps"
        :default-checked-keys="defkeys"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allRights"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Break from "../bread";
import roleAPT from "../../http/API/Role"
export default {
  data() {
    return {
      roleList: [],
      editRuleForm: {},
      dialogVisible: false, //编写弹出框
      addDialogVisible: false, //添加弹出框
      rightDialogVisible: false, //分配权限弹出框
      editRules: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      //   添加角色
      addRuleForm: {
        roleName: "",
        roleDesc: "",
      },
      rightList: [],
      //树形控件
      treeProps: {
        label: "authName",
        children: "children",
      },
      //默认选中权限得id值得数组
      defkeys: [],
      //即将分配
      roleId: "",
    };
  },
  components: {
    Break,
  },
  methods: {
    // 获取数据
    getRolesList() {
      roleAPT.getRolesList("roles").then((res) => {
        // console.log(res);
        this.roleList = res.data.data;
      });
    },
    //删除权限
    removeById(role, rightId) {
      // console.log(role,rightId);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          roleAPT.removeRightsById(this.roleId,this.rightId)
            .then((res) => {
              role.children = res.data.data;
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //删除角色
    removeDelRole(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          roleAPT.deleteRolesInfo(id).then((res) => {
            //   console.log(res)
            if (res.data.meta.status == 200) {
              this.$message.success("删除成功");
            } else {
              this.$message.error("删除失败");
            }
            this.getRolesList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 展示用户编辑的弹出框
    showEdidRole(id) {
      this.dialogVisible = true;
      this.editRuleForm = id;
    },
    //提交编辑
    editRoles() {
      this.$refs.ruleFormRef.validate((valid) => {
        if (valid) {
          this.$http
            .put("roles/" + this.editRuleForm.id, {
              roleName: this.editRuleForm.roleName,
              roleDesc: this.editRuleForm.roleDesc,
            })
            .then((res) => {
              if (res.data.meta.status == 200) {
                this.$message.success("修改成功");
              } else {
                this.$message.error("修改失败");
              }
              this.dialogVisible = false;
              this.getRolesList();
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //添加角色
    addRole() {
      this.$refs.ruleFormRef.validate((valid) => {
        if (valid) {
          roleAPT.addRoles(this.addRuleForm).then((res) => {
            console.log(res);
            if (res.data.meta.status == 201) {
              this.$message.success("添加角色成功");
            } else {
              this.$message.error("添加角色失败");
            }
            (this.addDialogVisible = false),
              this.getRolesList,
              (this.roleName = ""),
              (this.roleDesc = "");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //分配权限弹出框
    rightRole(row) {
      this.$http.get("rights/tree").then((res) => {
        this.rightDialogVisible = true;
        console.log(res);
        this.roleId = row.id;
        this.rightList = res.data.data;
      });
      this.getRecursion(row,this.defkeys)
    },
    //通过递归获取角色下所有三级权限得id
    getRecursion(a,arr){
        if(!a.children){
            return arr.push(a.id)
        }
        a.children.forEach(item => {
            this.getRecursion(item,arr)
        });
    },
    // setRightClosed(){
    //     this.defkeys=[]
    // },
    //确定分配权限
    allRights(){
        let keys=[
            ...this.$refs.treeRef.getCheckedKeys(),
            ...this.$refs.treeRef.getHalfCheckedKeys()
        ]
        let idStr=keys.join(",")
        roleAPT.allotRights(this.roleId,{
            rids:idStr,
        }).then(res=>{
            console.log(res)
            if(res.data.meta.status==200){
                this.$message.success("分配权限成功")
            }else{
                this.$message.error("分配权限失败")
            }
        })
        this.rightDialogVisible=false
        this.getRolesList()
    }
  },
  created() {
    this.getRolesList();
  },
};
</script>

<style scoped>
.box-card {
  margin-top: 20px;
}
</style>